package com.annet.token.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONValidator;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * @author yanhu
 * @version 2020/2/3 下午 01:20
 */
public final class Strings {


    public static boolean isNull(String str) {
        return null == str;
    }

    public static boolean isNotNull(String str) {
        return !isNull(str);
    }

    public static boolean isNotNull(String... strings) {
        if (null == strings || strings.length == 0) {
            return false;
        }
        for (String string : strings) {
            if (isNull(string)) {
                return false;
            }
        }
        return true;
    }

    public static boolean existNull(String... strings) {
        return !isNotNull(strings);
    }

    public static boolean isEmpty(String str) {
        return null == str || "".equals(str);
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isNotEmpty(String... strings) {
        if (null == strings || strings.length == 0) {
            return false;
        }
        for (String string : strings) {
            if (isEmpty(string)) {
                return false;
            }
        }
        return true;
    }

    public static boolean existEmpty(String... strings) {
        return !isNotEmpty(strings);
    }


    public static String randomIntStr(int len) {
        StringBuilder sb = new StringBuilder();
        ThreadLocalRandom random = ThreadLocalRandom.current();
        for (int i = 0; i < len; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    public static String randomStr(int len) {
        StringBuilder sb = new StringBuilder();
        ThreadLocalRandom random = ThreadLocalRandom.current();
        int c;
        for (int i = 0; i < len; i++) {
            switch (random.nextInt(3)) {
                case 0: // 0-9
                    c = random.nextInt(48, 57);
                    break;
                case 1: // A-Z
                    c = random.nextInt(65, 90);
                    break;
                default: // a-z
                    c = random.nextInt(97, 122);
            }
            sb.append((char) c);
        }
        return sb.toString();
    }

    public synchronized static String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static <T> Set<T> stringToSet(String idsStr, Class<T> clazz) {
        if (null != idsStr) {
            JSONValidator validator = JSONValidator.from(idsStr);
            if (validator.validate() && validator.getType() == JSONValidator.Type.Array) {
                JSONArray array = JSON.parseArray(idsStr);
                return new HashSet<>(array.toJavaList(clazz));
            }
        }
        return Collections.emptySet();
    }


    /**
     * 解析IP
     *
     * @param request HttpServletRequest
     * @return 请求IP地址
     */
    public static String parseIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (ip.equals("127.0.0.1")) {
                // 根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (inet != null) {
                    ip = inet.getHostAddress();
                }
            }
        }
        // 多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }
}
